package com.stylefeng.guns.modular.manager.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.model.Dept;
import com.stylefeng.guns.modular.system.model.GoodsCategory;
import com.stylefeng.guns.modular.system.dao.GoodsCategoryMapper;
import com.stylefeng.guns.modular.manager.service.IGoodsCategoryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.warpper.UserWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuXin
 * @since 2018-09-12
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements IGoodsCategoryService {

    @Override
    public void deleteCate(Integer cateId) {
        GoodsCategory goodsCategory = this.baseMapper.selectById(cateId);
        Wrapper<GoodsCategory> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("parent_ids", "%[" + goodsCategory.getId() + "]%");
        List<GoodsCategory> subDepts = this.baseMapper.selectList(wrapper);
        for (GoodsCategory temp : subDepts) {
            temp.deleteById();
        }
        goodsCategory.deleteById();
    }

    @Override
    public List<ZTreeNode> tree() {
        if(ShiroKit.isAdmin()){
            List<ZTreeNode> allData = this.baseMapper.listTree(null);
            return allData;
        }else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            List<ZTreeNode> treeNodes = this.baseMapper.listTree(dataScope);
            return treeNodes;
        }
    }

    @Override
    public List<Map<String, Object>> list(String condition) {
        if (ShiroKit.isAdmin()) {
            List<Map<String, Object>> result = baseMapper.listGoodsCategory(null, condition);
            return result;
        } else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            List<Map<String, Object>> result = baseMapper.listGoodsCategory(dataScope, condition);
            return result;
        }
    }
}
